﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;

namespace FrameDetectionTest
{
    public static class Common
    {
        public static void msDelay(int ms)
        {
            long StartTick;
            long EndTick;

            //delay == 0
            if (ms == 0) return;

            //delay != 0
            //开始的时间戳为现在的时间戳
            StartTick = DateTime.Now.Ticks;

            do
            {
                //Processes all Windows messages currently in the message queue.
                Application.DoEvents();
                EndTick = DateTime.Now.Ticks;
            } while ((EndTick - StartTick) < (ms * 10000));
        } // End msDelay
        public static void msFroceDelay(int ms)
        {
            long StartTick;
            long EndTick;

            StartTick = DateTime.Now.Ticks;
            do
            {
                //Application.DoEvents();
                EndTick = DateTime.Now.Ticks;
            } while ((EndTick - StartTick) < (ms * 10000));
        } // End msFroceDelay
        public static void SaveConfig(Settings Config)
        {
            try
            {
                //先获取当前config目录，若不存在则创建目录
                string ConfigDirectory = AppDomain.CurrentDomain.BaseDirectory + "Config";
                if (!Directory.Exists(ConfigDirectory))
                {
                    Directory.CreateDirectory(ConfigDirectory);
                }

                //config文件的总路径
                string ConfigPath = ConfigDirectory + "\\Config.json";


                //提供可确保正确使用 IDisposable 对象的方便语法(释放资源)
                using (StreamWriter sw = new StreamWriter(ConfigPath))
                {
                    string JsonStringConfig = JsonConvert.SerializeObject(Config);
                    sw.Write(JsonStringConfig);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static Settings LoadConfig()
        {
            Settings Config = null;
            try
            {
                string ConfigDirectory = AppDomain.CurrentDomain.BaseDirectory + "Config";
                if (!Directory.Exists(ConfigDirectory))
                {
                    Directory.CreateDirectory(ConfigDirectory);
                }
                string ConfigPath = ConfigDirectory + "\\Config.json";
                if (!File.Exists(ConfigPath))
                {
                    //则创建Config文件
                    Config = new Settings();
                    SaveConfig(Config);
                    return Config;
                }
                using (StreamReader sr = new StreamReader(ConfigPath))
                {
                    string JsonStringConfig = sr.ReadToEnd();
                    Config = JsonConvert.DeserializeObject<Settings>(JsonStringConfig);
                    sr.Close();
                    sr.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return Config;
        }
        public static void SaveProject(Project Project, string ProjectPath)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(ProjectPath))
                {
                    string JsonStringProject = JsonConvert.SerializeObject(Project);
                    sw.Write(JsonStringProject);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static Project LoadProject(string ProjectPath)
        {
            Project Project = null;
            try
            {
                using (StreamReader sr = new StreamReader(ProjectPath))
                {
                    string JsonStringProject = sr.ReadToEnd();
                    Project = JsonConvert.DeserializeObject<Project>(JsonStringProject);
                    sr.Close();
                    sr.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return Project;
        }
        public static void AddList(string LstPath,string ProjectName)
        {
            FileStream fs = new FileStream(LstPath, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(ProjectName);
            sw.Close();
            fs.Close();
        }
    }
}